<?php include('header.php'); ?>
<?php include('side.php'); ?>
	
	<div class="layui-body">
	
		<fieldset class="layui-elem-field layui-field-title" style="margin: 20px 0;">
			<legend><?= Lang::app('list') ?><span style="color: #BBA;font-size: 14px">（ <span id="count">0</span> 条记录）</span></legend>
		</fieldset>
	
		<table id="list" lay-filter="list"></table>
	</div>
	
	<script type="text/html" id="buttonList">
		<div class="layui-btn-group">
		{{ d.status == 0 ? '<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit" title="修改资料">\
				<i class="layui-icon layui-icon-edit"></i>\
			</button>' : '' }}
			<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="validate" title="{{ d.status == 1 ? '取消签到' : '确认签到' }}">
				<i class="layui-icon {{ d.status == 1 ? 'layui-icon-rate-solid' : 'layui-icon-rate' }}"></i>
			</button>
		</div>
	</script>
	
	<script>
	
		layui.use('table', function() {
			var table = layui.table,
			form = layui.form;
			var url = '/site/get_list';
			if (<?= $data['status'] ?> > -1) url += '/?s=<?= $data['status'] ?>';
			table.render({
				elem: '#list',
				url: url,
				cols: [[
					{field: 'id', width: 60, title: 'ID', sort: true},
					{field: 'name', width: 160, title: '姓名', sort: false, edit: 'text'},
					{field: 'table_name', width: 200, title: '桌号', sort: true},
					{field: 'mobile', width: 200, title: '手机号', sort: false, edit: 'text'/*, templet: function(res) {
						return windos.formatMobile(res.mobile, 0);
					}*/},
					{field: 'inviter', width: 140, title: '邀请人', sort: false, edit: 'text'},
					{field: 'status', width: 80, title: '状态', sort: true, templet: function(res) {
						if (res.status == 0) {
							return '<span style="color: #FF4303">未签到</span>';
						} else if(res.status == 1) {
							return '<span style="color: #0580FF">已签到</span>';
						} else {
							return '<span style="color: CCCCCC">出错' + res.status + '</span>'
						}
					}},
					{field: 'confirm_time', width: 180, title: '签到时间', sort: true, templet: function(res) {
						if (res.confirm_time) {
							return windos.formatDate(res.confirm_time, 0);
						} else {
							return '';
						}
						
					}},
					{fixed: 'right', width:180, title:'操作', toolbar: '#buttonList'}
				]],
				even: true,
				done: function(res, curr, count) {
					$('#count').html(count);
				}
			});
			
			table.on('edit(list)', function(obj) {
				var value = obj.value,
				data = obj.data,
				field = obj.field;
				var postdata = {id: data.id, field: field, value: value};
				windos.ajaxPost('/site/save', postdata, function(res) {
					if (res.code == 1) {
							layer.msg('操作成功');
						} else {
							layer.msg('操作失败');
						}
						console.log(res);
				});
				
			});
			
			form.on('switch(gender)', function(obj) {
				var value = this.value;
				if (obj.elem.checked) {
					value = 1;
				} else {
					value = 0;
				}
				var postdata = {id: this.value, field: this.name, value: value};
				windos.ajaxPost('/site/save', postdata, function(res) {
					if (res.code == 1) {
							layer.msg('操作成功');
						} else {
							layer.msg('操作失败');
						}
						console.log(res);
				});
			});
			
			table.on('tool(list)', function(obj) {
				var data = obj.data;
				var value;
				if (obj.event == 'validate') {
					value = data.status == 0 ? 1 : 0;
					var postdata = {id: data.id, field: 'status', value: value};
					windos.ajaxPost('/site/save', postdata, function(res) {
						if (res.code == 1) {
								table.reload('list');
								layer.msg('操作成功');
							} else {
								layer.msg('操作失败');
							}
							console.log(res);
					});
				}
				if (obj.event == 'edit') {
					location.href = '/site/entry?id=' + data.id;
				}
			});
		});
		
		
	</script>
<?php include('footer.php'); ?>
